package com.example.japinhands.ui.screen

import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import com.example.japinhands.JapViewModel
import com.example.japinhands.data.AdjData
import com.example.japinhands.data.DeformAdj
import com.example.japinhands.data.DeformVerb
import com.example.japinhands.data.VerbData

@Composable
fun DeformVerbScreen(
    viewModel: JapViewModel,
    update: Boolean,
    verb: DeformVerb? = null
) {
    val tag = "NewDeformVerbScreen"
    var verbData by remember {
        mutableStateOf(VerbData())
    }
    var deformVerb1 by remember {
        mutableStateOf(viewModel.deformVerbLiveData.value?.deform1 ?: "")
    }
    var deformVerb2 by remember {
        mutableStateOf(viewModel.deformVerbLiveData.value?.deform2 ?: "")
    }
    var deformVerb3 by remember {
        mutableStateOf(viewModel.deformVerbLiveData.value?.deform3 ?: "")
    }
    var deformVerb4 by remember {
        mutableStateOf(viewModel.deformVerbLiveData.value?.deform4 ?: "")
    }
    var deformVerb5 by remember {
        mutableStateOf(viewModel.deformVerbLiveData.value?.deform5 ?: "")
    }
    var deformVerb6 by remember {
        mutableStateOf(viewModel.deformVerbLiveData.value?.deform6 ?: "")
    }
    var deformVerb7 by remember {
        mutableStateOf(viewModel.deformVerbLiveData.value?.deform7 ?: "")
    }
    var deformVerb8 by remember {
        mutableStateOf(viewModel.deformVerbLiveData.value?.deform8 ?: "")
    }
    var deformVerb9 by remember {
        mutableStateOf(viewModel.deformVerbLiveData.value?.deform9 ?: "")
    }
    var deformVerb10 by remember {
        mutableStateOf(viewModel.deformVerbLiveData.value?.deform10 ?: "")
    }
    var deformVerb11 by remember {
        mutableStateOf(viewModel.deformVerbLiveData.value?.deform11 ?: "")
    }
    var deformVerb12 by remember {
        mutableStateOf(viewModel.deformVerbLiveData.value?.deform12 ?: "")
    }
    val saveNewDeform: () -> Unit = {
        val deform = DeformVerb(
            deform1 = deformVerb1,
            deform2 = deformVerb2,
            deform3 = deformVerb3,
            deform4 = deformVerb4,
            deform5 = deformVerb5,
            deform6 = deformVerb6,
            deform7 = deformVerb7,
            deform8 = deformVerb8,
            deform9 = deformVerb9,
            deform10 = deformVerb10,
            deform11 = deformVerb11,
            deform12 = deformVerb12
        )
        viewModel.saveDeformVerb(deform) {
            viewModel.allowSubmit = true
        }
    }

    val handler by remember {
        mutableStateOf(object : Handler(Looper.getMainLooper()) {
            override fun handleMessage(msg: Message) {
                if (msg.what == 123) saveNewDeform.invoke()
            }
        })
    }

    val afterChange: () -> Unit = {
        viewModel.allowSubmit = false
        handler.apply {
            removeCallbacksAndMessages(null)
            sendEmptyMessageDelayed(123, 1000)
        }
    }

    LaunchedEffect(verb) {
        verbData = getUIData(update, verb, viewModel)
        deformVerb1 = verbData.d1
        deformVerb2 = verbData.d2
        deformVerb3 = verbData.d3
        deformVerb4 = verbData.d4
        deformVerb5 = verbData.d5
        deformVerb6 = verbData.d6
        deformVerb7 = verbData.d7
        deformVerb8 = verbData.d8
        deformVerb9 = verbData.d9
        deformVerb10 = verbData.d10
        deformVerb11 = verbData.d11
        deformVerb12 = verbData.d12
        saveNewDeform.invoke()
    }

    Column {
        LeftRightItem(
            leftLabel = "原型",
            rightLabel = "ます型",
            leftText = deformVerb1,
            rightText = deformVerb2,
            onLeftChange = { deformVerb1 = it; afterChange.invoke() },
            onRightChange = { deformVerb2 = it; afterChange.invoke() }
        )
        LeftRightItem(
            leftLabel = "て型",
            rightLabel = "た型",
            leftText = deformVerb3,
            rightText = deformVerb4,
            onLeftChange = { deformVerb3 = it; afterChange.invoke() },
            onRightChange = { deformVerb4 = it; afterChange.invoke() }
        )
        LeftRightItem(
            leftLabel = "ない型",
            rightLabel = "ば型",
            leftText = deformVerb5,
            rightText = deformVerb6,
            onLeftChange = { deformVerb5 = it; afterChange.invoke() },
            onRightChange = { deformVerb6 = it; afterChange.invoke() }
        )
        LeftRightItem(
            leftLabel = "命令型",
            rightLabel = "意志型",
            leftText = deformVerb7,
            rightText = deformVerb8,
            onLeftChange = { deformVerb7 = it; afterChange.invoke() },
            onRightChange = { deformVerb8 = it; afterChange.invoke() }
        )
        LeftRightItem(
            leftLabel = "可能型",
            rightLabel = "被动型",
            leftText = deformVerb9,
            rightText = deformVerb10,
            onLeftChange = { deformVerb9 = it; afterChange.invoke() },
            onRightChange = { deformVerb10 = it; afterChange.invoke() }
        )
        LeftRightItem(
            leftLabel = "使役型",
            rightLabel = "被动使役型",
            leftText = deformVerb11,
            rightText = deformVerb12,
            onLeftChange = { deformVerb11 = it; afterChange.invoke() },
            onRightChange = { deformVerb12 = it; afterChange.invoke() }
        )
//        Button(
//            modifier = Modifier.fillMaxWidth(),
//            onClick = {
//                Log.d(tag, "deformVerb1: $deformVerb1")
//                Log.d(tag, "deformVerb2: $deformVerb2")
//                Log.d(tag, "deformVerb3: $deformVerb3")
//                Log.d(tag, "deformVerb4: $deformVerb4")
//                Log.d(tag, "deformVerb5: $deformVerb5")
//                Log.d(tag, "deformVerb6: $deformVerb6")
//                Log.d(tag, "deformVerb7: $deformVerb7")
//                Log.d(tag, "deformVerb8: $deformVerb8")
//                Log.d(tag, "deformVerb9: $deformVerb9")
//                Log.d(tag, "deformVerb10: $deformVerb10")
//                Log.d(tag, "deformVerb11: $deformVerb11")
//                Log.d(tag, "deformVerb12: $deformVerb12")
//            }
//        ) {
//            Text(text = "Submit")
//        }
    }
}

private fun getUIData(update: Boolean, verb: DeformVerb?, viewModel: JapViewModel) = if (update) {
    var res = VerbData()
    verb?.let {
        res = VerbData(it)
    }
    res
} else {
    VerbData(
        viewModel.deformVerbLiveData.value?.deform1 ?: "",
        viewModel.deformVerbLiveData.value?.deform2 ?: "",
        viewModel.deformVerbLiveData.value?.deform3 ?: "",
        viewModel.deformVerbLiveData.value?.deform4 ?: "",
        viewModel.deformVerbLiveData.value?.deform5 ?: "",
        viewModel.deformVerbLiveData.value?.deform6 ?: "",
        viewModel.deformVerbLiveData.value?.deform7 ?: "",
        viewModel.deformVerbLiveData.value?.deform8 ?: "",
        viewModel.deformVerbLiveData.value?.deform9 ?: "",
        viewModel.deformVerbLiveData.value?.deform10 ?: "",
        viewModel.deformVerbLiveData.value?.deform11 ?: "",
        viewModel.deformVerbLiveData.value?.deform12 ?: "",
    )
}